package com.nf.health.app.intervention_details.fragment;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.intervention_details.bean.SportSchemeVo;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.TextUtilTools;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 运动方案详情fragment
 * Created by Administrator on 2016/11/10.
 */
public class ExercisedetailsFragment extends IBaseFragment {

    SportSchemeVo sportSchemeVo = new SportSchemeVo();
    @Bind(R.id.tv_scheme_common_head_title)
    TextView tvSchemeCommonHeadTitle;//运动名称
    @Bind(R.id.tv_scheme_common_head_content)
    TextView tvSchemeCommonHeadContent;//运动详情
    @Bind(R.id.duanliananpai)//运动周期
            TextView duanliananpai;
    @Bind(R.id.exercised_image)//运动图片
            ImageView exercisedImage;
    @Bind(R.id.exercised_blod_title)//运动名称
            TextView exercisedBlodTitle;
    @Bind(R.id.sport_type)//运动类型
            TextView sportType;
    @Bind(R.id.sport_time)//运动时间
            TextView sportTime;
    @Bind(R.id.sport_qiangdu)//运动强度
            TextView sportQiangdu;
    @Bind(R.id.gongxiaolistview)
    TextView gongxiaolistview;
    @Bind(R.id.zhuyilistview)
    TextView zhuyilistview;
    @Bind(R.id.dongzuolistview)
    TextView dongzuolistview;
    @Bind(R.id.jianyi)
    TextView jianyi;
    @Bind(R.id.exerc_lin)
    LinearLayout exerc_lin;
    @Bind(R.id.exe_lin)
    LinearLayout exeLin;
    @Bind(R.id.exe_lin2)
    LinearLayout exeLin2;
    @Bind(R.id.exe_lin3)
    LinearLayout exeLin3;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_exercisedetails);
        ButterKnife.bind(this, rootView);
        initView();
    }

    private void initView() {
        String id = Constants.CHANGE_ABLE.INTERVENTION_ID;
        provider.requesexercised(id, "sportdetails");
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if ("sportdetails".equals(actionName)) {
            sportSchemeVo = (SportSchemeVo) object;

            tvSchemeCommonHeadTitle.setText("运动方案");
            tvSchemeCommonHeadContent.setText(sportSchemeVo.getGuide());


            duanliananpai.setText("一周监测安排表" + "(" + DateUtil.dateToString(new Date(Long.parseLong(sportSchemeVo.getStarttime())), "yyyy-MM-dd") + "至" + DateUtil.dateToString(new Date(Long.parseLong(sportSchemeVo.getEndtime())), "yyyy-MM-dd") + ")");

            ImageLoaderUtils.getinstance(getActivity()).getImage(exercisedImage, sportSchemeVo.getPicture(), R.drawable.default_image);


            exercisedBlodTitle.setText("-" + sportSchemeVo.getName() + "-");
            sportType.setText(sportSchemeVo.getType());
            sportTime.setText(sportSchemeVo.getSporttime());
            sportQiangdu.setText(sportSchemeVo.getSportstrength());
            if (TextUtils.isEmpty(sportSchemeVo.getSuggest())) {
                exerc_lin.setVisibility(View.GONE);
            } else {
                exerc_lin.setVisibility(View.VISIBLE);
                jianyi.setText(sportSchemeVo.getSuggest());
            }


            String StrS = sportSchemeVo.getEffect();

            if (StrS.contains("||")) {
                String result[] = StrS.split("\\|\\|");
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < result.length; i++) {
                    //sb.append("<font color='#2E86F4'>" + (i + 1) + "." + "</font>" + result[i] + "<br>");
                    sb.append(result[i] + "\n");
                }
                if(TextUtils.isEmpty(sb.toString())){
                    gongxiaolistview.setText("");
                }else {
                    gongxiaolistview.setText(sb.toString());
                }
            }else{
                if (TextUtils.isEmpty(sportSchemeVo.getEffect())) {
                    exeLin.setVisibility(View.GONE);
                } else {
                    exeLin.setVisibility(View.VISIBLE);
                    gongxiaolistview.setText(StrS);
                }
            }
            String StrS1 = sportSchemeVo.getAttention();

            if (StrS1.contains("||")) {
                String result[] = StrS1.split("\\|\\|");
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < result.length; i++) {
                    //sb.append("<font color='#2E86F4'>" + (i + 1) + "." + "</font>" + result[i] + "<br>");
                    sb.append(result[i] + "\n");
                }

                if(TextUtils.isEmpty(sb.toString())){
                    zhuyilistview.setText("");
                }else {
                    zhuyilistview.setText(sb.toString());
                }
            }else{
                if (TextUtils.isEmpty(sportSchemeVo.getAct())) {
                    exeLin2.setVisibility(View.GONE);
                } else {
                    exeLin2.setVisibility(View.VISIBLE);
                    zhuyilistview.setText(StrS1);
                }
            }
            String StrS2 = sportSchemeVo.getAct();

            if (StrS2.contains("||")) {
                String result[] = StrS2.split("\\|\\|");
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < result.length; i++) {
                    //sb.append("<font color='#2E86F4'>" + (i + 1) + "." + "</font>" + result[i] + "<br>");
                    sb.append(result[i] + "\n");
                }

                if(TextUtils.isEmpty(sb.toString())){
                    dongzuolistview.setText("");
                }else {
                    dongzuolistview.setText(sb.toString());
                }
            }else{
                if (TextUtils.isEmpty(sportSchemeVo.getAttention())) {
                    exeLin3.setVisibility(View.GONE);
                } else {
                    exeLin3.setVisibility(View.VISIBLE);
                    dongzuolistview.setText(StrS2);
                }
            }
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
